//
//  BIDNewsListViewController.m
//  cxgov
//
//  Created by mal on 16/3/15.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDNewsListViewController.h"
#import "BIDNewsDetailViewController.h"
#import "BIDWebPageJumpViewController.h"

//列表
static NSString *newsListURL = @"newsAndroidAction!getNewsList.action";

@interface BIDNewsListViewController ()<BIDBaseListViewDelegate>

@end

@implementation BIDNewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"新闻";
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], newsListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld&id=%@", self.curPage, self.countPerPage, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier = @"identifier";
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    cell.imageView.image = [UIImage imageNamed:@"news_flag.png"];
    cell.textLabel.text = dic[@"title"];
    cell.textLabel.font = [UIFont systemFontOfSize:15.f];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
//    BIDNewsDetailViewController *vc = [[BIDNewsDetailViewController alloc] initWithNibName:@"BIDNewsDetailViewController" bundle:nil];
//    vc.newsTitle = dic[@"title"];
//    vc.newsDate = dic[@"createTime"];
//    vc.newsContent = dic[@"content"];
    BIDWebPageJumpViewController *vc = [[BIDWebPageJumpViewController alloc] initWithNibName:@"BIDWebPageJumpViewController" bundle:nil];
    vc.desURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"fileUrl"]];
    vc.title = @"新闻";
    [self.navigationController pushViewController:vc animated:YES];
}

@end
